Ext.define('DCApp.controller.DetailController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.detail',

    requires: [
        'Ext.window.Toast'
    ],
    config: {
        control: {
            'button[text=插入]': {
                click: 'onInsert'
            }
        }
    },
    onInsert:function(btn,e){
    	var vw = this.getView();
    	var he = vw.down('htmleditor');
    	var dv = btn.up('panel').down('dataview');
    	var sels = dv.getSelectionModel().getSelection();
    	var text="";
    	for(var i=0; i<sels.length; i++){
    		var url = sels[i].get('uri');
    		if(url && url.indexOf('.jpg')!=-1 || url.indexOf('.png')!=-1)
    			text+='<img style="float:left;margin:5px;" src="'+url+'"/>';
    		else
    			text+='<a href="'+url+'">'+ sels[i].get('name')+'</a>';
    	}
    	he.insertAtCursor(text);
    	he.focus();
    },
    onBackClick: function() {
    	var nd = this.getView().getXType().replace('detail','list');
    	this.redirectTo(nd);
    },    
    onSaveClick: function() {
    	var title = this.getView().title;
        var form = this.lookupReference('form'),rec;  
        if(!form.isValid()) {
        	return false;
        }
        rec = this.getViewModel().get('theObj');
        Ext.Msg.wait('保存', '保存'+title+'中...');
        rec.save({
            scope: this,
            callback: this.onComplete
        });
    },

    onComplete: function() {
    	var vw = this.getView();
    	var title = vw.title;
        Ext.Msg.hide();
        Ext.toast({
            title: '保存',
            html: title+'保存完成',
            align: 't',
            bodyPadding: 10
        });
        //判断,有返回按钮才返回
        var btn_return = vw.down('button[text=返回]');
        if(!btn_return.isHidden())
        	this.onBackClick();
        else{
        	vw.up('window').close();
        }
    },
    //获得仿真数据总数
    getEntityCount:function(){
    	var vm = this.getViewModel();
    	var cn  = Ext.getClassName(vm.data.theObj);
    	var pos=cn.lastIndexOf('.');
    	return DCApp.Cfg.SIM_ENM[cn.substring(pos+1)];
    },
    onNext: function() {
  	 	this.fireViewEvent('objnext');    	 	
   },
    onPrev: function() {
		this.fireViewEvent('objprev');    	
    	
    }
    
});
